#include "stdafx.h"
#include "Helper.h"
#include <shellapi.h>
#include "MainDlg.h"
CHelper::CHelper()
{
}


CHelper::~CHelper()
{
}

BOOL CHelper::ParseCommandLine(std::vector<std::wstring> &vecOut)
{
	LPCWSTR pszArgCmd = GetCommandLine();
	int nArgCount = 0;
	LPWSTR *pszArgCmdArry = CommandLineToArgvW(pszArgCmd, &nArgCount);

	for (int i = 0; i < nArgCount; i++)
	{
		vecOut.push_back(pszArgCmdArry[i]);
	}
	for (auto it : vecOut)
	{
		transform(it.begin(), it.end(), it.begin(), tolower);
		if (it.compare(_T("-regall")) == 0)
		{
			//CMainDlg::FileAssociate(TRUE, FALSE);
			return TRUE;
		}
		else if (it.compare(_T("-unregall")) == 0)
		{
			//CMainDlg::FileAssociate(FALSE, FALSE);
			return TRUE;
		}
	}


	return FALSE;
}

BOOL CHelper::SendCommandLine(HWND hWnd, std::vector<std::wstring> &veccmdln)
{
	if (veccmdln.empty()) {
		return FALSE;
	}
	int bufflen = 0;
	for (int i = 1; i < veccmdln.size(); i++)
	{
		bufflen += static_cast<int>(veccmdln[i].length()) + 1;
	}
	void *pBuf = new wchar_t[bufflen];
	memset(pBuf, 0, bufflen);
	wchar_t *p = (wchar_t *)pBuf;
	for (int j = 1; j < veccmdln.size(); j++)
	{
		int nLen = static_cast<int>(veccmdln[j].length()) + 1 ;
		memcpy(p, veccmdln[j].c_str(), nLen * sizeof(TCHAR));
		p += nLen;
	}
	
	COPYDATASTRUCT cds;
	cds.dwData = 0x6ABE51;
	cds.cbData = bufflen * sizeof(TCHAR);
	cds.lpData = (void*)(BYTE*)pBuf;
	bool bRet =  SendMessage(hWnd, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds);
	delete[]pBuf;
	return bRet;
}
